# coding: utf-8
class UserBll

	# 获取邮箱认证加密串
	def UserBll.getEmailValidCode(uid, email)
		v0 = uid.to_s << '__' << email
		v1 = Digest::MD5.hexdigest(v0)
		v2 = v1.reverse
		v2[2, 12]
	end

	# 获取一个新的邮箱认证码
	# currentCode - 当前用户现有的认证码
	def UserBll.getEmailCertcode(currentCode)
		return currentCode if(2 & currentCode == 2)
		2 + currentCode
	end

	# 获取邮箱认证的url地址
	def UserBll.getEmailCertUrl(uid, email)
		token = UserBll.getEmailValidCode(uid, email)
		'http://www.ttbiji.com/user/validemail/?source=regmail&uid=' << uid.to_s << '&email=' << email << '&token=' << token
	end

	# 格式化邮箱地址
	def UserBll.formatEmail(email)
		arr = email.split('@')
		if(arr.length == 2)
			name = arr[0]
			other = arr[1]
			name[rand(name.length)] = '*'
			name[rand(name.length)] = '*'
			name[rand(name.length)] = '*'
			return name << '@' << other
		end
		email
	end

	# 获取某个用户的密码重置加密串
	# uid : 用户编号
	# addtime : 入库时间
	def UserBll.getResetPassCode(uid, addtime)
		_add = addtime.in_time_zone(+8.hours).strftime('%H:%M:%S')
		_now = Time.now.in_time_zone(+8.hours).strftime('%Y-%m-%d')
		str = uid.to_s << '_' << _add << '_' << _now
		v1 = Digest::MD5.hexdigest(str)
		v2 = v1.reverse
		v2[2, 16]
	end

	# 获取重新设置密码的url地址
	# uid : 用户编号
	# addtime : 入库时间
	def UserBll.getResetPassUrl(uid, addtime)
		token = UserBll.getResetPassCode(uid, addtime)
		"http://www.ttbiji.com/user/resetpass?uid=#{uid.to_s}&token=#{token}"
	end

end